﻿using Microsoft.Xna.Framework.Graphics;
using RenderingLibrary.Content;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RenderingLibrary.Content
{
    /// <summary>
    /// Provides a simple implementation of IContentLoader for applications
    /// using the LoaderManager and not specifying their own custom ContentLoader.
    /// This content loader uses the default SystemManagers internally.
    /// </summary>
    public class ContentLoader : IContentLoader
    {
        public SystemManagers SystemManagers { get; set; }

        Dictionary<string, IDisposable> disposables = new Dictionary<string,IDisposable>();

        public T LoadContent<T>(string contentName)
        {
            if(typeof(T) == typeof(Texture2D))
            {
                var texture = LoaderManager.Self.Load(contentName, SystemManagers);
                return (T)(object)texture;
            }
            else
            {
                throw new NotImplementedException();
            }
        }

        public void DisposeAndClear()
        {
            foreach(var item in disposables.Values)
            {
                item.Dispose();
            }

            disposables.Clear();
        }

        public T TryGetCachedDisposable<T>(string contentName)
        {
            if(disposables.ContainsKey(contentName))
            {
                return (T)disposables[contentName];
            }
            else
            {
                return default(T);
            }
        }

        public void AddDisposable(string contentName, IDisposable disposable)
        {
            if (disposables.ContainsKey(contentName))
            {
                throw new Exception("This item has already been added");
            }
            else
            {
                disposables.Add(contentName, disposable);
            }
        }
    }
}
